package com.tq68.weixin.controller.login;

import java.util.Date;

import com.jfinal.core.Controller;
import com.tq68.weixin.common.AppConstantEnum;
import com.tq68.weixin.model.Bbs;
import com.tq68.weixin.model.LoginKeep;
import com.tq68.weixin.model.Seller;
import com.tq68.weixin.utils.DateUtils;
import com.tq68.weixin.utils.MD5;
import com.tq68.weixin.vo.SellerVO;

public class SellerLoginController extends Controller {

	public void index(){
		String ttype = getPara("ttype","1");
		setAttr("ttype",ttype);
		String openid = getPara("openid","");
		setAttr("openid",openid);
		if(!"".equals(openid)){
			LoginKeep lk = LoginKeep.dao.findByOpenid(openid,Integer.parseInt(ttype));
			if(lk!=null){
				setAttr("keepUsername",lk.getUsername());
				setAttr("keepPassword",lk.getPassword());
			}
		}
		render("index.html");
	}
	
	public void login(){
		String passwd = getPara("passwd","");
		String username = getPara("username","");
		Integer ttype = getParaToInt("ttype",1);
		String openid = getPara("openid","");
		try{
			passwd = MD5.encrypt(passwd);
		}catch(Exception e){
			e.printStackTrace();
		}
		Seller seller = Seller.dao.getSellerByLogin(username, passwd, ttype);
		int data = 0;
		if(seller != null){//info matched
			data = seller.getId();
			if(!"".equals(openid)){
				LoginKeep lk = LoginKeep.dao.findByOpenid(openid,ttype);
				if(lk != null){
					lk.setUsername(username);
					lk.setPassword(getPara("passwd",""));
					lk.setIskeep(1);
					lk.setTtype(ttype);
					lk.setLastUpdateTime(DateUtils.toTimeStr(new Date()));
					lk.update();
				}else{
					lk = new LoginKeep();
					lk.setOpenid(openid);
					lk.setUsername(username);
					lk.setPassword(getPara("passwd",""));
					lk.setIskeep(1);
					lk.setTtype(ttype);
					lk.setLastUpdateTime(DateUtils.toTimeStr(new Date()));
					lk.save();
				}
			}
		}
		renderText(data+"");
	}
	
	public void loginSuccess(){
		Integer id = getParaToInt("sellerid",0);
		Integer ttype = getParaToInt("ttype",1);
		Seller seller = Seller.dao.findByAttr(id);
		SellerVO sellervo = new SellerVO();
		sellervo.setId(seller.getId());
		sellervo.setAccount(seller.getSellerAccount());
		sellervo.setSellerName(seller.getSellerName());
		sellervo.setLogo(getRequest().getContextPath()+AppConstantEnum.SELLER_IMG_PATH+seller.getSellerLogo());
		sellervo.setShortAddress(seller.getSellerShortAddress());
		sellervo.setLevelNum(seller.getSellerLevel());
		sellervo.setLevelStr(AppConstantEnum.SellerLevel.codeMap.get(seller.getSellerLevel()+""));
		sellervo.setCoins(seller.getCoinCount());
		sellervo.setCoinsPerBbs(AppConstantEnum.SELLER_PER_COST);
		sellervo.setCommuid(seller.getInt("commid"));
		sellervo.setCommuName(seller.getStr("commName"));
		sellervo.setSellerType(seller.getSellerType());
		sellervo.setSellerTypeStr(AppConstantEnum.SellerType.codeMap.get(seller.getSellerType()+""));
		sellervo.setAllowedDeployNum(AppConstantEnum.DISCOUNT_PER_DAY);
		sellervo.setDeployedNum(Bbs.dao.getDeployedNumBySeller(sellervo.getId()));
		setSessionAttr("sellervo", sellervo);
		removeSessionAttr("uservo");
		if(ttype==1){
			redirect("/discounts");
		}else if(ttype==0){
			redirect("/admini");
		}
	}
}
